package ihm.panel.camera;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import ihm.applet.KrakenApplet;
import ihm.panel.KrakenTablePanel;
import ihm.panel.PanelInterface;
import ihm.table.AppletTable;
import ihm.table.AppletTableModel;

import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;

import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;

import bdd.standalone.bean.Camera;
import bdd.standalone.bean.Video;

//import bdd.external.bean.Camera;
//import bdd.external.bean.Video;

/**
 * Panneau qui affiche les vidéos liées à une caméra.
 * @author Guillaume Pocas & Jean-Fraçois Augé
 */
public class VideoPanel extends KrakenTablePanel implements PanelInterface, ActionListener {
	
	private static final long serialVersionUID = -261495215873449491L;
	private KrakenApplet applet;
	private Camera camera;
	
	private CameraPanel camera_jp;
	
	private int total_videos, new_videos;
	private static String[] COLUMN_NAMES = {"ID", "Camera", "Nom", "Heure début", "Heure fin", "Jour début", "Jour Fin", "Poids"};
	private AppletTableModel video_atm;
	private JTable video_jt;
	private Object[][] column_content;
	
	private JPanel bottom_jp;
	private JButton read_jb;
	
	/**
	 * Constructeur.
	 * @param app
	 * @param cam_jp
	 */
	public VideoPanel(KrakenApplet app, CameraPanel cam_jp){
		this.applet = app;
		this.camera_jp = cam_jp;
		this.updateTableInfo();
		this.initPanel();
	}

	@Override
	public void initPanel() {
		/*
		 * Table part!
		 */
		setLayout(new BorderLayout());
		video_atm = new AppletTableModel(COLUMN_NAMES, column_content);
		for(int i = 0; i < 8; i++)	video_atm.lockColumn(i);
		video_jt = new AppletTable(video_atm);
		
		JScrollPane scrollPane = new JScrollPane(video_jt);
		add(scrollPane, BorderLayout.CENTER);
		
		/*
		 * Buttons part!
		 */
		bottom_jp = new JPanel();
		
		read_jb = new JButton("Lire la vidéo");
		read_jb.addActionListener(this);
		bottom_jp.add(read_jb);

		add(bottom_jp, BorderLayout.SOUTH);
	}
	
	@Override
	public void updateTableInfo() {
		//Session session = applet.getExternalSession();
		Session session = applet.getSession();

		camera = this.camera_jp.getNodePanel().getCurrentCamera();
		@SuppressWarnings("unchecked")
		List<Video> resultats = session.createCriteria(Video.class).add(Restrictions.eq("camera", this.camera)).list();		
		column_content = new Object[resultats.size()][COLUMN_NAMES.length];
		
		int i;
		for(i = 0; i < resultats.size(); i++) {
			column_content[i][0] = resultats.get(i).getVideo_id();
			column_content[i][1] = resultats.get(i).getCamera();
			column_content[i][2] = resultats.get(i).getVideo_name();
			column_content[i][3] = resultats.get(i).getVideo_timeBegin();
			column_content[i][4] = resultats.get(i).getVideo_timeEnd();
			column_content[i][5] = resultats.get(i).getVideo_dayBegin();
			column_content[i][6] = resultats.get(i).getVideo_dayEnd();
			column_content[i][7] = resultats.get(i).getVideo_weight();
			//setValue du model pour afficher en temps reel
		}
		new_videos = i - total_videos;
		total_videos = i;
		
		session.close();
	}
	
	public Camera getCamera() { return this.camera; }

	public AppletTableModel getAppletTableModel() { return this.video_atm; }
	
	public Object[][] getColumnContent() { return this.column_content; }

	@Override
	public void actionPerformed(ActionEvent arg0) {
		// TODO
	}

}
